package cn.tedu.powerfulfitness.service.impl;

import cn.tedu.powerfulfitness.ex.ServiceException;
import cn.tedu.powerfulfitness.mapper.ProductMapper;
import cn.tedu.powerfulfitness.pojo.dto.ProductAddNewDTO;
import cn.tedu.powerfulfitness.pojo.dto.ProductUpdateDTO;
import cn.tedu.powerfulfitness.pojo.entity.Product;
import cn.tedu.powerfulfitness.pojo.vo.ProductListItemVO;
import cn.tedu.powerfulfitness.pojo.vo.ProductStandardVO;
import cn.tedu.powerfulfitness.service.IProductService;
import cn.tedu.powerfulfitness.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addNew(ProductAddNewDTO productAddNewDTO) {
        // 从参数对象中获取商品名称
        String title = productAddNewDTO.getTitle();
        // 调用productMapper的int countByTitle(String title)根据名称执行统计
        int countByTitle = productMapper.countByTitle(title);
        // 判断统计结果是否大于0
        if (countByTitle > 0) {
            // 是：抛出异常
            String message = "添加商品失败，商品名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 创建product对象
        Product product = new Product();
        // 调用BeanUtils.copyProperties()将参数对象的属性值复制到product对象中
        BeanUtils.copyProperties(productAddNewDTO, product);
        // 调用productMapper的int insert(product product)方法将商品数据插入到数据库
        int rows = productMapper.insert(product);
        if (rows != 1) {
            String message = "添加商品失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Integer id) {
        ProductStandardVO queryResult = productMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除商品失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = productMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除商品失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Integer id, ProductUpdateDTO productUpdateDTO) {
        ProductStandardVO queryResult = productMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "修改商品详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String title = productUpdateDTO.getTitle();
        int count = productMapper.countByTitleAndNotId(id, title);
        if (count > 0) {
            String message = "修改商品详情失败，商品名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDTO, product);
        product.setId(id);
        int rows = productMapper.update(product);
        if (rows != 1) {
            String message = "修改商品详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public ProductStandardVO getStandardById(Integer id) {
        ProductStandardVO queryResult = productMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取商品详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;
    }

    @Override
    public List<ProductListItemVO> list() {
        List<ProductListItemVO> list = productMapper.list();
        return list;
    }

}
